#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <signal.h>



#define BUFF_LEN 100

#include "mychar.h"

int fd = 0;
void signal_handler(int signo);
int main(int argc, char *argv[]){
	int flg = 0;
	int ret = 0;

	if(argc < 2){
		printf("too few arguments\n");
		return 1;
	}

	fd = open(argv[1], O_RDWR);

	if(fd < 0){
		printf("open failed : %s \n", argv[1]);
		return 2;
	}

	//将该进程设置成该文件描述符的拥有者
	fcntl(fd, F_SETOWN, getpid());
	//启用该文件描述符的异步IO
	flg = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, flg | O_ASYNC);

	//注册信号处理函数
	signal(SIGIO, signal_handler);

	while(1){
	}

	close(fd);
	return 0;
}

void signal_handler(int signo){
	char buff[BUFF_LEN] = "";
	read(fd, buff, BUFF_LEN);
	printf("read buff %s \n", buff);
}
